package cn.edu.scau.cmi.wuzewei.spring.client;

import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;

import cn.edu.scau.cmi.wuzewei.spring.domain.Student;
import cn.edu.scau.cmi.wuzewei.spring.util.ApplicationContextUtil;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

public class SpringClient extends Application {

	@Override
	public void start(Stage primaryStage) throws Exception {

		Group group = new Group();
		
		Label label1 = new Label("学生姓名拼音：");
		label1.setLayoutX(100);
		label1.setLayoutY(100);
		group.getChildren().add(label1);
		
		TextField stuNameTextField = new TextField();
		stuNameTextField.setText("caixukun");
		stuNameTextField.setLayoutX(190);
		stuNameTextField.setLayoutY(95);
		group.getChildren().add(stuNameTextField);
		
		TextArea textArea = new TextArea();
		textArea.setMaxWidth(300);
		textArea.setLayoutX(90);
		textArea.setLayoutY(130);
		group.getChildren().add(textArea);
		
		Button button = new Button("查询");
		button.setLayoutX(340);
		button.setLayoutY(95);
		button.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				String s = stuNameTextField.getText().trim();
				textArea.setText("");
				ApplicationContext applicationContext = ApplicationContextUtil.getApplicationContext();
				try {
					Student student = (Student) applicationContext.getBean(s);
					textArea.appendText("姓名:" + student.getName() + "\r\n");
					textArea.appendText("性别:" + student.getGendar() + "\r\n");
					textArea.appendText("指导教师:" + student.getTutor().getName());
				}catch(NoSuchBeanDefinitionException e) {
					textArea.setText("该学生不存在");
				}
			}
		});
		group.getChildren().add(button);
				
		Scene scene = new Scene(group);
		primaryStage.setTitle("实验八：Spring框架");
		primaryStage.setWidth(500);
		primaryStage.setHeight(400);
		primaryStage.setScene(scene);
		primaryStage.show();

	}

	public static void main(String[] args) {
		launch(args);
	}
}
